package com.unlcn.erp.hyc.interfaces.backend.service;

import cn.huiyunche.commons.exception.BusinessException;
import com.alibaba.fastjson.JSONObject;
import com.unlcn.erp.hyc.interfaces.backend.bo.ImportHistoryDB;
import com.unlcn.erp.hyc.interfaces.backend.enums.CallHYCUrlTypeEnum;
import com.unlcn.erp.hyc.interfaces.backend.util.HttpRequestUtil;
import com.unlcn.erp.hyc.interfaces.base.model.Hycwaybilldetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 调用慧运车接口
 * Created by zhiche024 on 2017/9/6.
 */
@Service
public class CallHYCUrlReceiveServiceImpl implements CallHYCUrlReceiveService{

    private static final Logger LOGGER = LoggerFactory.getLogger(CallHYCUrlReceiveServiceImpl.class);

    private static final String WAYBILL = "WAYBILL" ,GETWAYBILLDETAIL = "GETWAYBILLDETAIL",ORDER = "ORDER" ,ORDERDETAILS = "ORDERDETAILS" ,SUBMITSHIP = "SUBMITSHIP",RECCODES = "RECCODES" ,RECDETAIL = "RECDETAIL",PAYCODES = "PAYCODES" ,PAYDETAIL = "PAYDETAIL";;

    @Autowired
    private CallHYCUrlReceiveLogService callHYCUrlReceiveLogService;

    @Value("${hyc.bound.host.url}")
    private String propertyUrl;

    @Value("${hyc.bound.host.timeout}")
    private String propertyTime;

    @Override
    public String callHYCUrlReceive(String tag,String date) throws Exception{
        String url = propertyUrl;
        Integer time = Integer.parseInt(propertyTime);
        String result = null;
        Map<String, Object> map = new HashMap<String, Object>();
        ImportHistoryDB db = new ImportHistoryDB();
        db.setDtcallstart(new Date());
        if(Objects.equals(tag , ORDER)){
            url = url + "/api/order/getOrderCodes";
            result = HttpRequestUtil.sendHttpGet(url,null,time);
            db.setHycUrlType((short) CallHYCUrlTypeEnum.HYC_ORDERCODES.getValue());
        }else if(Objects.equals(tag , ORDERDETAILS)){
            url = url + "/api/order/getOrderDetail";
            map.put("orderCode",date);
            result = HttpRequestUtil.sendHttpPost(url,null,map,time);
            db.setHycUrlType((short) CallHYCUrlTypeEnum.HYC_ORDERDETAILS.getValue());
        }else if(Objects.equals(tag , WAYBILL)){
            url = url + "/api/waybill/getWaybillCodes";
            result = HttpRequestUtil.sendHttpGet(url,null,time);
            db.setHycUrlType((short) CallHYCUrlTypeEnum.HYC_WAYBILLS.getValue());
        }else if(Objects.equals(tag , GETWAYBILLDETAIL)){
            url = url + "/api/waybill/getWaybillDetail";
            map.put("waybillCode",date);
            result = HttpRequestUtil.sendHttpPost(url,null,map,time);
            db.setHycUrlType((short) CallHYCUrlTypeEnum.HYC_WAYBILLDETAILS.getValue());
        }else if(Objects.equals(tag , SUBMITSHIP)){
            url = url + "/api/waybill/submitShipTo";
            Hycwaybilldetail hycwaybilldetail = JSONObject.parseObject(date,Hycwaybilldetail.class);
            map.put("waybillCode",hycwaybilldetail.getWaybillcode());
            map.put("orderCode",hycwaybilldetail.getOrdercode());
            map.put("shipToDate",hycwaybilldetail.getDtarrive());
            result = HttpRequestUtil.sendHttpPost(url,null,map,time);
            db.setHycUrlType((short) CallHYCUrlTypeEnum.HYC_WAYBILLDETAILS.getValue());
        }else if(Objects.equals(tag , RECCODES)){
            url = url + "/api/bms/bill/getReceivableCodes";
            result = HttpRequestUtil.sendHttpGet(url,null,time);
            db.setHycUrlType((short) CallHYCUrlTypeEnum.HYC_RECEIVABLECODES.getValue());
        }else if(Objects.equals(tag , RECDETAIL)){
            url = url + "/api/bms/bill/getReceivableDetail";
            map.put("billCode",date);
            result = HttpRequestUtil.sendHttpPost(url,null,map,time);
            db.setHycUrlType((short) CallHYCUrlTypeEnum.HYC_RECEIVABLECODESDETAIL.getValue());
        }else if(Objects.equals(tag , PAYCODES)){
            url = url + "/api/bms/bill/getPayableCodes";
            result = HttpRequestUtil.sendHttpGet(url,null,time);
            db.setHycUrlType((short) CallHYCUrlTypeEnum.HYC_PAYCODES.getValue());
        }else if(Objects.equals(tag , PAYDETAIL)){
            url = url + "/api/bms/bill/getPayableDetail";
            map.put("billCode",date);
            result = HttpRequestUtil.sendHttpPost(url,null,map,time);
            db.setHycUrlType((short) CallHYCUrlTypeEnum.HYC_PAYDETAIL.getValue());
        }

        db.setDtcallend(new Date());
        db.setHycUrl(url);
        //解析返回结果
        if(!Objects.equals(result , null)){
            JSONObject jsonObject = JSONObject.parseObject(result);
            Boolean success = jsonObject.getBoolean("success");
            String messageCode = jsonObject.getString("messageCode");
            String message = jsonObject.getString("message");
            String data = jsonObject.getString("data");
            if(!success){
                LOGGER.error("CallOrderByHYCServiceImpl.callTms error msg: {}", message);
                throw new BusinessException(message);
            }
            //调用接口信息写入URL日志表中
            db.setSuccess((short)((success)?1:0));
            db.setPullOrPush((short) 0);
            db.setResultContent(data);
            db.setMessagecode(messageCode);
            db.setMessage(message);
            //LOGGER.
            callHYCUrlReceiveLogService.saveCallHYCUrlReceiveLog(db);
            return data;
        }
        return null;
    }


    @Override
    public Boolean backHYCUrlReceive(String tag,Hycwaybilldetail bean) throws Exception{
        String url = propertyUrl;
        Integer time = Integer.parseInt(propertyTime);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String result = null;
        Map<String, Object> map = new HashMap<String, Object>();
        ImportHistoryDB db = new ImportHistoryDB();
        db.setDtcallstart(new Date());
        if(Objects.equals(tag , SUBMITSHIP)){
            url = url + "/api/waybill/submitShipTo";
//            Hycwaybilldetail hycwaybilldetail = bean.class;
            map.put("waybillCode",bean.getWaybillcode());
            map.put("serviceOrderCode",bean.getOrdercode());
            map.put("shipToDate",sdf.format(bean.getDtarrive()));
            result = HttpRequestUtil.sendHttpPost(url,null,map,time);
            db.setHycUrlType((short) CallHYCUrlTypeEnum.HYC_WAYBILLDETAILS.getValue());
        }
        db.setDtcallend(new Date());
        db.setHycUrl(url);
        //解析返回结果
        if(!Objects.equals(result , null)){
            JSONObject jsonObject = JSONObject.parseObject(result);
            Boolean success = jsonObject.getBoolean("success");
            String messageCode = jsonObject.getString("messageCode");
            String message = jsonObject.getString("message");
            String data = jsonObject.getString("data");
            if(!success){
                LOGGER.error("CallOrderByHYCServiceImpl.callTms error msg: {}", message);
                throw new BusinessException(message);
            }
            //调用接口信息写入URL日志表中
            db.setSuccess((short)((success)?1:0));
            db.setPullOrPush((short) 0);
            db.setResultContent(data);
            db.setMessagecode(messageCode);
            db.setMessage(message);
            callHYCUrlReceiveLogService.saveCallHYCUrlReceiveLog(db);
            return success;
        }
        return false;
    }
}
